Here are a few more examples illustrating different scenarios with Jagged arrays in Java:
Creating and Displaying a Jagged Array
Creating and Displaying a Jagged Array
public class Main{
public static void main(String[] args) {
int[][] jaggedArray = {
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};
// Displaying elements of the jagged array
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println();
}
}
}
Output
1 2 3
4 5
6 7 8 9
This code initializes and displays a jagged array jaggedArray with different lengths of sub-arrays (inner arrays). It directly assigns different lengths of arrays for the rows.
Access and display jagged array
Access and display jagged array
public class Main{
public static void main(String[] args) {
// Declaration and initialization of a jagged array
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[]{1, 2};
jaggedArray[1] = new int[]{3, 4, 5, 6};
jaggedArray[2] = new int[]{7, 8, 9};
// Accessing elements in the jagged array
System.out.println("Element at [0][1]: " + jaggedArray[0][1]); // Output: 2
System.out.println("Element at [1][2]: " + jaggedArray[1][2]); // Output: 5
}
}
Output
Element at [0][1]: 2
Element at [1][2]: 5
This code declares, initializes, and accesses specific elements in a jagged array jaggedArray. It demonstrates how to access elements by their indices in a jagged array.
Jagged Array for Representing a List of Student Grades
Representing a List of Student Grades using jagged array
public class Main{
public static void main(String[] args) {
// Create a jagged array to store grades for multiple students in different subjects
int[][] jaggedArray = new int[3][];
// Initialize each inner array with different lengths
jaggedArray[0] = new int[3]; // Student 1 grades
jaggedArray[1] = new int[2]; // Student 2 grades
jaggedArray[2] = new int[4]; // Student 3 grades
// Assign grades to each student
jaggedArray[0][0] = 90; // Student 1, Math grade
jaggedArray[0][1] = 85; // Student 1, Science grade
jaggedArray[0][2] = 92; // Student 1, English grade
jaggedArray[1][0] = 88; // Student 2, Math grade
jaggedArray[1][1] = 91; // Student 2, Science grade
jaggedArray[2][0] = 75; // Student 3, Math grade
jaggedArray[2][1] = 82; // Student 3, Science grade
jaggedArray[2][2] = 94; // Student 3, English grade
jaggedArray[2][3] = 100; // Student 3, Art grade
// Print the student grades
for (int student = 0; student < 3; student++) {
System.out.print("Student " + (student + 1) + ": ");
for (int grade : jaggedArray[student]) {
System.out.print(grade + " ");
}
System.out.println();
}
}
}
Output
Student 1: 90 85 92
Student 2: 88 91
Student 3: 75 82 94 100
This code represents a jagged array jaggedArray to store grades for multiple students in different subjects. It initializes inner arrays with varying lengths to accommodate different subjects for each student.
Each of these examples showcases the concept of jagged arrays in Java, demonstrating different scenarios like displaying elements, accessing specific values, and storing student grades in a structured manner. Running these code snippets will produce their respective outputs as described above.